﻿using Biz;
using BusinessLayer.Biz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BusinessLayer.Dal
{
    public class ReceiptDal
    {
        DatabaseContext db;
        public ReceiptDal( /* DatabaseContext db */ )
        {
            this.db = DatabaseContext.DB;
        }
        public List<ReceiptNote> GetAll()
        {
            return db.ReceiptNotes.ToList();
        }
        public List<ReceiptNote> GetAllByDate(DateTime from, DateTime to)
        {
            return db.ReceiptNotes.Where(o => o.Time >= from && o.Time <= to).ToList();
        }
        public void Insert(ReceiptNote receipt, List<ReceiptDetail> details)
        {
            db.ReceiptNotes.Add(receipt);
            db.SaveChanges();
            foreach (var item in details)
            {
                item.ReceiptNote = receipt;
                item.Insert();
                Unsold unsold = new Unsold();
                unsold.Product = item.Product;
                unsold.QuantityExport = 0;
                unsold.QuantityUnsold = unsold.GetQuantityByProduct(item.Product) + item.Quantity;
                unsold.QuantityImport = item.Quantity;
                unsold.Time = DateTime.Now;
                unsold.Insert();
            }
        }
    }
}
